package com.koudai.reader.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.koudai.reader.vo.Book;
import com.koudai.reader.vo.BookUpdateCheck;
import com.koudai.reader.vo.EndUser;
import com.koudai.reader.vo.Task;

public class JsonUtil {

    public static List<Task> convertTasklist(JSONArray listJson) throws JSONException {
        List<Task> list = new ArrayList<Task>();
        
        if (listJson != null && listJson.length() > 0) {
            for (int i = 0; i < listJson.length(); i++) {
                Task task = new Task();
                JSONObject taskJson = listJson.getJSONObject(i);
                task.setId(taskJson.getString("id"));
                task.setContent(taskJson.getString("content"));
                task.setStatus(taskJson.getString("status"));
                list.add(task);
            }
        }
        
        return list;
    }
    
    
    public static List<Book> convertUpdateBooklist(JSONArray listJson) throws JSONException {
        List<Book> list = new ArrayList<Book>();
        
        if (listJson != null && listJson.length() > 0) {
            for (int i = 0; i < listJson.length(); i++) {
                Book book = new Book();
                JSONObject bookJson = listJson.getJSONObject(i);
                book.setBookid(Integer.valueOf(bookJson.getString("id")));
                book.setServerLastChapter(bookJson.getString("last"));
                list.add(book);
            }
        }
        
        return list;
    }
    
    public static void convert2UserInfo(JSONObject user) throws JSONException {
        EndUser.getInstance().setUid(user.getString("uid"));
        EndUser.getInstance().setGold(user.getInt("gold"));
        EndUser.getInstance().setBookCount(user.getInt("book_count"));
        EndUser.getInstance().setVip(user.getString("is_vip").equals("1") ? true : false);
    }
    
    public static List<Book> convertBooklist (JSONArray booklistJson) throws JSONException {
        List<Book> list = new ArrayList<Book>();
        
        if (booklistJson != null && booklistJson.length() > 0) {
            for (int i = 0; i < booklistJson.length(); i++) {
                Book book = new Book();
                JSONObject bookJson = booklistJson.getJSONObject(i);
                book.setAuthor(bookJson.getString("author"));
                book.setBookid(Integer.valueOf(bookJson.getString("id")));
                book.setCateid(Integer.valueOf(bookJson.getString("cateId")));
                book.setDownloadPath(bookJson.getString("file"));
                book.setServerLastChapter(bookJson.getString("last"));
                book.setSummary(bookJson.getString("summary"));
                book.setTitle(bookJson.getString("title"));
                list.add(book);
            }
        }
        
        return list;
    } 
    
    public static void convertMainCategory(JSONObject catelistJson) throws JSONException {
        Iterator<String> iterator = catelistJson.keys();
        while (iterator.hasNext()) {
            String key = iterator.next(); 
            String value = catelistJson.getString(key);
            Constant.mainCategoryMapping.put(Integer.valueOf(key), value);
        }
        
    }
    
    public static void convertAllCategory(JSONArray catelistJson) throws JSONException {

        if (catelistJson != null && catelistJson.length() > 0) {
            for (int i = 0; i < catelistJson.length(); i++) {
                JSONObject item = catelistJson.getJSONObject(i);
                Constant.allCategoryMapping.put(item.getString("cateName"), Integer.valueOf(item.getInt("cateId")));
            }
        }
    }
    
    public static List<BookUpdateCheck> convertBookUpdate(JSONArray bookupdates) throws JSONException{
        List<BookUpdateCheck> result = new ArrayList<BookUpdateCheck>();
        
        if (bookupdates != null && bookupdates.length() > 0) {
            for (int i = 0; i < bookupdates.length(); i++) {
                JSONObject item = bookupdates.getJSONObject(i);
                BookUpdateCheck bookupdate = new BookUpdateCheck();
                bookupdate.setBookid(Integer.valueOf(item.getString("id")));
                bookupdate.setLastChapter(item.getString("last"));
                result.add(bookupdate);
            }
        }
        
        return result;
    }

    public static String convertTermlist(JSONArray jsonArray) throws JSONException {
        StringBuilder sb = new StringBuilder();
        sb.append("功能-----------普通----VIP<br/><br/>");
        if (jsonArray != null && jsonArray.length() > 0) {
            for (int i = 0; i < jsonArray.length(); i++) {
                sb.append(jsonArray.getJSONObject(i).get("term") + "<br/><br/>");
            }
        }
        
        return sb.toString();
    }
}
